<template>
  <div id="listQuestionSubmit">
    <a-card style="margin-bottom: 20px" hoverable>
      <a-form :model="queryParams" layout="inline">
        <a-form-item field="questionId" label="问题ID">
          <a-input
            v-model="queryParams.questionId"
            placeholder="请输入问题ID"
            :style="{ width: '150px', marginRight: '20px' }"
          />
        </a-form-item>
        <a-form-item field="userId" label="用户ID">
          <a-input
            v-model="queryParams.userId"
            placeholder="请输入用户ID"
            :style="{ width: '150px', marginRight: '20px' }"
          />
        </a-form-item>
        <a-form-item field="language" label="编程语言">
          <a-select
            v-model="queryParams.language"
            :style="{ width: '150px' }"
            placeholder="选择编程语言"
          >
            <a-option>java</a-option>
            <a-option>c</a-option>
            <a-option>cpp</a-option>
            <a-option>go</a-option>
            <a-option>html</a-option>
            <a-option>python</a-option>
          </a-select>
        </a-form-item>
        <a-form-item field="status" label="判题状态">
          <a-select
            v-model="queryParams.status"
            :style="{ width: '150px' }"
            placeholder="选择判题状态"
          >
            <a-option :value="0">待判题</a-option>
            <a-option :value="1">判题中</a-option>
            <a-option :value="2">成功</a-option>
            <a-option :value="3">失败</a-option>
          </a-select>
        </a-form-item>
        <a-form-item>
          <a-button type="primary" @click="handleSearch">搜索</a-button>
        </a-form-item>
      </a-form>
    </a-card>
    <a-table
      :columns="columns"
      :data="data"
      :pagination="{
        showTotal: true,
        pageSize: queryParams.pageSize,
        current: queryParams.current,
        total: Number(total),
      }"
      @pageChange="handlePageChange"
    >
      <template #title="{ record }">
        <a-tooltip :content="`问题ID:${record.questionVO.id}`">
          <a-button
            type="text"
            @click="router.push(`/detail/question/${record.questionVO.id}`)"
          >
            {{ record.questionVO.title }}
          </a-button>
        </a-tooltip>
      </template>
      <template #username="{ record }">
        <a-tooltip :content="`用户ID:${record.userVO.id}`">
          <a-button type="text">{{ record.userVO.username }}</a-button>
        </a-tooltip>
      </template>
      <template #tags="{ record }">
        <a-space wrap>
          <a-tag
            v-for="(tag, index) of record.questionVO.tags"
            :key="index"
            color="green"
            disabled
            >{{ tag }}
          </a-tag>
        </a-space>
      </template>
      <template #judgeInfo="{ record }">
        {{ record.judgeInfo.message ?? "暂无信息" }}
      </template>
      <template #code="{ record }">
        {{ record.code ?? "您无权限查看" }}
      </template>
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD HH:mm:ss") }}
      </template>
      <!--      <template #userInfo="{ record }">-->
      <!--        {{ record.userVO }}-->
      <!--      </template>-->
      <!--      <template #question="{ record }">-->
      <!--        {{ record.questionVO }}-->
      <!--      </template>-->
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { ref, watchEffect } from "vue";
import { QuestionControllerService, QuestionSubmitQueryRequest } from "@/api";
import message from "@arco-design/web-vue/es/message";
import moment from "moment";
import router from "@/router";

const data = ref([]);
const total = ref(0);
const queryParams = ref<QuestionSubmitQueryRequest>({
  current: 0,
  pageSize: 10,
});

const loadData = async () => {
  const res = await QuestionControllerService.listQuestionSubmitByPageUsingPost(
    {
      ...queryParams.value,
      sortField: "create_time",
      sortOrder: "descend",
    }
  );
  if (res.code === 0) {
    data.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("系统错误" + res.message);
  }
};

//监听表格的变化
watchEffect(() => {
  loadData();
});

const columns = [
  {
    title: "ID",
    dataIndex: "id",
    width: 60,
  },
  {
    title: "标题",
    slotName: "title",
  },
  {
    title: "用户名称",
    slotName: "username",
    width: 100,
  },
  {
    title: "标签",
    slotName: "tags",
  },
  {
    title: "编程语言",
    dataIndex: "language",
    width: 100,
  },
  {
    title: "状态",
    dataIndex: "status",
    width: 70,
  },
  {
    title: "判题信息",
    slotName: "judgeInfo",
    width: 140,
  },
  {
    title: "作答代码",
    slotName: "code",
    ellipsis: true,
    tooltip: true,
  },
  {
    title: "创建时间",
    slotName: "createTime",
  },
  // {
  //   title: "用户详情",
  //   slotName: "userInfo",
  // },
  // {
  //   title: "问题详情",
  //   slotName: "question",
  // },
];

const handlePageChange = (page: number) => {
  queryParams.value = {
    ...queryParams.value,
    current: page,
  };
};
const handleSearch = () => {
  loadData();
};
</script>

<style scoped>
#listQuestionSubmit {
  max-width: 1200px;
  margin: 0 auto;
}
</style>
